///<reference path="file://D:/Android/touch/sencha-touch-all-debug-w-comments.js" />

var extObjectsMap = Ext.create("Ext.util.HashMap");
Ext.namespace("keni.html");
keni.html.UIRender = function () {

    function createExtFields(fieldsMetadataArray) {
        var extFields = new Array();
        Ext.Array.each(fieldsMetadataArray, function (fieldMetadata, index, array) {
           // alert(fieldMetadata.fieldType);
            try {
                var extField = Ext.create("Ext.field." + fieldMetadata.fieldType, {});

                for (property in fieldMetadata) {

                    setPropertities(extField, property, fieldMetadata[property]);
                }
                extFields[index] = extField;
                extObjectsMap.add(fieldMetadata.htmlElementId, extField);
            }
            catch (error) {
                alert(error);
            }

        });
        return extFields;
    }

    function setPropertities(extObj, propertyName, propertyValue) {
        var methodName = "set" + toUpperForTheFirstChar(propertyName);
        // alert(methodName);
        if (extObj[methodName]) {
            if (isNum(propertyValue) || isBoolean(propertyValue)) {
                //alert(propertyValue);
                eval("extObj." + methodName + "(" + propertyValue + ")");
                return;
            }
            extObj[methodName](propertyValue);
        }

    }
    function toUpperForTheFirstChar(word) {
        return word.substring(0, 1).toUpperCase() + word.substring(1);
    }

    function isBoolean(value) {
        if (value != null && value != "") {
            var tempValue = value.toLowerCase();
            if (tempValue == "false" || tempValue == "true") {
                return true;
            }
        }
        return false;
    }

    function isNum(value) {

        if (value != null && value != "") {
            return !isNaN(value);
        }
        return false;

    }

    function createExtFieldSet(fieldSetMetadataArray) {
        var extFieldSets = new Array();
        Ext.Array.each(fieldSetMetadataArray, function (fieldSetMetadata, index, array) {
            alert("fieldset");
            var extFieldSet = Ext.create("Ext.form.FieldSet", {});
            for (property in fieldSetMetadata) {

                setPropertities(extFieldSet, property, fieldSetMetadata[property]);

            }
            extFieldSets[index] = extFieldSet;
            extFieldSet.add(createExtFields(fieldSetMetadata.fields));
            extObjectsMap.add(fieldSetMetadata.htmlElementId, extFieldSet);
        });
        return extFieldSets;

    }

    function createExtFormPanel(panelMetadataArray) {

        Ext.Array.each(panelMetadataArray, function (panelMetadata, index, array) {
            var extPanel = Ext.create("Ext.form.Panel");
           // alert("panel");
            for (property in panelMetadata) {
              
                   // alert(property);
                    setPropertities(extPanel, property, panelMetadata[property]);
        
            }
                alert(panelMetadata.fields);
            extPanel.add(createExtFieldSet(panelMetadata.fieldSets));
            extPanel.add(createExtFields(panelMetadata.fields));
            extObjectsMap.add(panelMetadata.htmlElementId, extPanel);
        });
    }

    this.render = function (pageMetadata) {
        //alert(pageMetadata);
        var pageMetadataObj = Ext.JSON.decode(pageMetadata);

        createExtFormPanel(pageMetadataObj.panels);
        createExtFields(pageMetadataObj.fields);
    }
    this.getProperty = function (htmlElementId, propertyName) {
        var element = extObjectsMap.get(htmlElementId);
        var realPropertyName = "get" + toUpperForTheFirstChar(propertyName);
        if (element[realPropertyName]) {
            return element[realPropertyName]();
        }
        throw propertyName + " is not definded in this object."

    }

    this.setProperty = function (htmlElementId, propertyName, propertyValue) {
        var element = extObjectsMap.get(htmlElementId);
        var realPropertyName = "set" + toUpperForTheFirstChar(propertyName);
        if (element[realPropertyName]) {
            setPropertities(element, propertyName, propertyValue);
        }
        throw propertyName + " is not definded in this object."

    }


}

keni.html.DataCollector = function () {

    function collectDataFromUI() {
        var dataObj = {};

        extObjectsMap.each(function (key, value, length) {
            if (value["getValue"]) {
                dataObj[key] = value.getValue();
            }
        });
        return Ext.JSON.encode(dataObj);
    }

    this.saveToBackend = function () {
        window.UIMetadata.saveToBackend(collectDataFromUI());
    }
}


